{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 列表\r\n",
    "\r\n",
    "Python的**列表（list）**对象是最常用的**序列**(Sequence)。与字符串是不可变序列不同，**列表是可变的**。可通过对偏移量进行修改和读取。\r\n",
    "\r\n",
    "### 列表赋值\r\n",
    "\r\n",
    "列表可通过索引对其对应的元素进行赋值，从而改变列表的内容，如：\r\n",
    "\r\n",
    "```python\r\n",
    ">>> a = [2, 2, 2]\r\n",
    ">>> a[1] = 1\r\n",
    ">>> print(a)\r\n",
    "[2, 1, 2]\r\n",
    "```\r\n",
    "\r\n",
    "通过上述代码的运行，我们可以看到列表确实是可以改变的。\r\n",
    "\r\n",
    "除了使用方括号的方式外，还可以使用list()函数来创建列表。例如：\r\n",
    "\r\n",
    "```python\r\n",
    "name = list('Python')\r\n",
    "```\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "list1 = [1,2,3,'zifuchuan']\r\n",
    "print(list1,list1[0])"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[1, 2, 3, 'zifuchuan'] 1\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "\r\n",
    "### 删除元素\r\n",
    "\r\n",
    "使用`del`语句可以删除列表中的元素，如：\r\n",
    "\r\n",
    "```python\r\n",
    ">>> a = [2, 2, 2]\r\n",
    ">>> del a[1]\r\n",
    ">>> print(a)\r\n",
    "[2, 2]\r\n",
    "```\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "del list1[0]\r\n",
    "print(list1)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[3, 'zifuchuan']\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 分片赋值\r\n",
    "\r\n",
    "分片赋值可以一次为多个元素赋值，并且不用考虑原列表的长度是否和新的列表长度一直，如：\r\n",
    "\r\n",
    "```python\r\n",
    ">>> name = list('Python')\r\n",
    ">>> print(name)\r\n",
    "['P', 'y', 't', 'h', 'o', 'n']\r\n",
    ">>> name[2:] = list('data')\r\n",
    ">>> print(name)\r\n",
    "['P', 'y', 'd', 'a', 't', 'a']\r\n",
    "```\r\n",
    "\r\n",
    "上述代码中的`list`函数是Python内置函数，其作用是将字符串转换为列表。\r\n",
    "\r\n",
    "运行结果显示，通过分片赋值，将原有列表`['P', 'y', 't', 'h', 'o', 'n']`，修改为`['P', 'y', 'd', 'a', 't', 'a']`。\r\n",
    "\r\n",
    "分片赋值还可以用来插入元素，如：\r\n",
    "\r\n",
    "```python\r\n",
    ">>> name = list('Python')\r\n",
    ">>> name[1:1] = list('--')\r\n",
    ">>> print(name)\r\n",
    "['P', '-', '-', 'y', 't', 'h', 'o', 'n']\r\n",
    "```\r\n",
    "\r\n",
    "结果显示将原有列表`['P', 'y', 't', 'h', 'o', 'n']`，修改为`['P', '-', '-', 'y', 'd', 'a', 't', 'a']`。"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "list2 = list('xbmu.edu.cn')\r\n",
    "list2[4:] = list('.org')\r\n",
    "print(list2)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "['x', 'b', 'm', 'u', '.', 'o', 'r', 'g']\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### 插入元素\r\n",
    "\r\n",
    "insert方法用于将对象插入到列表中，例如：\r\n",
    "\r\n",
    "```python\r\n",
    "a = [1, 2, 3]\r\n",
    "a.insert(2, 2.5)\r\n",
    "print(a)\r\n",
    "```\r\n",
    "\r\n",
    "运行结果为[1, 2, 2.5, 3]，insert方法的两个参数值很好理解，第一个参数为在哪个元素前插入，表示位置，第二个参数为插入的内容。"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "a = [1, 2, 3]\r\n",
    "a.insert(2, 2.5)\r\n",
    "print(a)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[1, 2, 2.5, 3]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "help(a.insert)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.9.7",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.7 64-bit ('sda-2020-1': venv)"
  },
  "interpreter": {
   "hash": "4637a9784c399fcab3c04262ccb84c20f81b8b61e1166e9dac843a98de3261f4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}